//
//  IntegralInfoViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/12.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "IntegralInfoViewController.h"

@interface IntegralInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UIView *footerView;

@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) NSDictionary *dataDictionary;

@end

//网络
@implementation IntegralInfoViewController (NetWorking)

/**
 限时几分
 */
- (void)getTimeData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"auId":[UserModel sharedInstanced].userId};
    [[AFAppDotNetAPIClient sharedClient] POST:@"LimInteStatisties/selectByauIdLI.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            self.dataArray = responseObject[@"obj"];
            [self.view addSubview:self.tableView];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

/**
 永久积分
 */
- (void)getEverData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"auId":[UserModel sharedInstanced].userId};
    [[AFAppDotNetAPIClient sharedClient] POST:@"PerInteStatistics/selectByauIdPI.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            self.dataArray = responseObject[@"obj"];
            [self.view addSubview:self.tableView];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

//UI
@implementation IntegralInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    switch (self.type) {
        case integralType_time:
            self.navigationItem.title = @"限时积分";
            [self getTimeData];
            break;
        case integralType_ever:
            self.navigationItem.title = @"永久积分";
            [self getEverData];
            break;
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IntegralInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([IntegralInfoTableViewCell class])];
    cell.type = self.type;
    cell.dataDictionary = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark - set & get

- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footerView;
        _tableView.tableHeaderView = self.headerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.rowHeight = (self.type == integralType_time ? 55 : 70);

        _tableView.separatorInset = UIEdgeInsetsMake(0, 22, 0, 22);
        _tableView.layoutMargins = UIEdgeInsetsMake(0, 22, 0, 22);
        [_tableView registerClass:[IntegralInfoTableViewCell class] forCellReuseIdentifier:NSStringFromClass([IntegralInfoTableViewCell class])];
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 80/WIDTH_5S_SCALE)];
        _headerView.backgroundColor = [UIColor whiteColor];
        UIView *boardView = [[UIView alloc] initWithFrame:CGRectMake(40, 20/WIDTH_5S_SCALE, SCREEN_WIDTH-80, 40/WIDTH_5S_SCALE)];
        boardView.layer.borderWidth = 3;
        boardView.layer.borderColor = (self.type == integralType_time ? getColor(@"ff0000") : getColor(@"1b81d0")).CGColor;
        
        UILabel *alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10/WIDTH_5S_SCALE, SCREEN_WIDTH-80, 30/WIDTH_5S_SCALE)];
        alertLabel.text = @"您 剩 余 的 积 分 余 额 为";
        alertLabel.font = DEF_FontSize_16;
        alertLabel.textAlignment = NSTextAlignmentCenter;
        
        CALayer *lineLayer = [CALayer layer];
        lineLayer.frame = CGRectMake(0, _headerView.height - 5, SCREEN_WIDTH, 5);
        lineLayer.backgroundColor = getColor(@"f8f8f8").CGColor;
        
        UILabel *integralLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
        integralLabel.backgroundColor = [UIColor whiteColor];
        integralLabel.center = CGPointMake(SCREEN_WIDTH/2., 20/WIDTH_5S_SCALE);
        integralLabel.font = [UIFont systemFontOfSize:25 weight:1];
        integralLabel.textAlignment = NSTextAlignmentCenter;
        integralLabel.textColor = (self.type == integralType_time ? getColor(@"ff0000") : getColor(@"1b81d0"));

        NSString *tmp = self.integral;
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:tmp];
        [attStr addAttribute:NSFontAttributeName value:DEF_FontSize_12 range:[tmp rangeOfString:@"分"]];
        integralLabel.attributedText = attStr;
        
        [_headerView.layer addSublayer:lineLayer];
        [boardView addSubview:alertLabel];
        [_headerView addSubview:boardView];
        [_headerView addSubview:integralLabel];
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[];
    }
    return _dataArray;
}

- (NSDictionary *)dataDictionary{
    if (!_dataDictionary) {
        _dataDictionary = @{};
    }
    return _dataDictionary;
}

@end
